package com.sxt.pro.controller;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxt.pro.pojo.ProductType;
import com.sxt.pro.service.ProductTypeService;
import com.sxt.pro.vo.ProductTypeVo;
import com.sxt.sys.common.AppFileUtils;
import com.sxt.sys.common.Constast;
import com.sxt.sys.common.DataGridView;
import com.sxt.sys.common.ResultObj;
import com.sxt.sys.common.WebUtils;
import com.sxt.sys.domain.Terminal;
import com.sxt.sys.service.TerminalService;


/**  

* @Description: 产品类型控制层

* @author xinxiaokang  

* @date 2020年8月24日 上午11:32:12

*/
@RestController
@RequestMapping("/productType")
public class ProductTypeController {

	@Autowired
	private ProductTypeService productTypeService;
	@Autowired
	private TerminalService terminalService;
	
	/**
	 * 查询产品类型及分页列表
	 * @param productTypeVo
	 * @return
	 */
	@RequestMapping("/loadAllProductType")
	public DataGridView loadAllProductType(ProductTypeVo productTypeVo){
		//存入网点编号和用户名放入session中
		 int banknbr = (int) WebUtils.getSession().getAttribute("banknbr");
		 productTypeVo.setBanknbr(banknbr);
		//初始化 当前页、每页显示条数
		IPage<ProductType> page = new Page<>(productTypeVo.getPage(), productTypeVo.getLimit());
		//封装对象查询条件
		QueryWrapper<ProductType> queryWrapper = new QueryWrapper<ProductType>();
		//筛选条件：网点编号
		  queryWrapper.eq(productTypeVo.getBanknbr()!=null&&productTypeVo.getBanknbr()!=0,"banknbr",productTypeVo.getBanknbr());
		//筛选条件:产品类型名称
		queryWrapper.like(StringUtils.isNotBlank(productTypeVo.getProducttypename()), "producttypename", productTypeVo.getProducttypename());
		//筛选条件：大于开始时间
		queryWrapper.ge(productTypeVo.getStartTime()!=null, "producttypetime", productTypeVo.getStartTime());
		//筛选条件：小于结束时间
		queryWrapper.le(productTypeVo.getEndTime()!=null, "producttypetime", productTypeVo.getEndTime());
		queryWrapper.orderByDesc("producttypetime");
		//获取分页数据
		this.productTypeService.page(page, queryWrapper);
		return new DataGridView(page.getTotal(), page.getRecords());
	}
	
	/**
	 * 添加产品类型
	 * @param productTypeVo
	 * @return
	 */
	@RequestMapping("addProductType")
	public ResultObj addProductType(ProductTypeVo productTypeVo) {
		//添加时，网点默认
		int banknbr = (int) WebUtils.getSession().getAttribute("banknbr");
		 productTypeVo.setBanknbr(banknbr);
		try {
			if(productTypeVo.getProducttypeimage()!=null&&productTypeVo.getProducttypeimage().endsWith("_temp")) {
				//图片名称
				String newName=AppFileUtils.renameFile(productTypeVo.getProducttypeimage());
				productTypeVo.setProducttypeimage(newName);
			}
			productTypeVo.setProducttypetime(new Date());
			this.productTypeService.save(productTypeVo);
			return ResultObj.ADD_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.ADD_ERROR;
		}
	}

	/**
	 * 修改产品类型
	 * @param productTypeVo
	 * @return
	 */
	@RequestMapping("updateProductType")
	public ResultObj updateProductType(ProductTypeVo productTypeVo) {
		try {
			//说明是不默认图片
			if(!(productTypeVo.getProducttypeimage()!=null&&productTypeVo.getProducttypeimage().equals(Constast.IMAGES_DEFAULTGOODSIMG_PNG))) {
				if(productTypeVo.getProducttypeimage().endsWith("_temp")) {
					String newName=AppFileUtils.renameFile(productTypeVo.getProducttypeimage());
					productTypeVo.setProducttypeimage(newName);
					//删除原先的图片
					String oldPath=this.productTypeService.getById(productTypeVo.getId()).getProducttypeimage();
					if (!(oldPath.equals(Constast.IMAGES_DEFAULTGOODSIMG_PNG))) {
						AppFileUtils.removeFileByPath(oldPath);
					}
				}
			}
			this.productTypeService.updateById(productTypeVo);
			return ResultObj.UPDATE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.UPDATE_ERROR;
		}
	}

	/**
	 * 删除产品类型
	 * @param id
	 * @param producttypeimage
	 * @return
	 */
	@RequestMapping("deleteProductType")
	public ResultObj deleteProductType(Integer id,String producttypeimage) {
		try {
			//删除原文件
			if (!producttypeimage.isEmpty() && !producttypeimage.equals("images/defaultgoodsimg.png")) {
				AppFileUtils.removeFileByPath(producttypeimage);
			}
			this.productTypeService.removeById(id);
			return ResultObj.DELETE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.DELETE_ERROR;
		}
	}
	
	/**
	 * 查询产品类型名称列表
	 * @return
	 */
	@RequestMapping("loadAllProductTypeForSelect")
	public DataGridView loadAllProductTypeForSelect(){
		ProductType productType = new ProductType();
		//存入网点编号和用户名放入session中
		 int banknbr = (int) WebUtils.getSession().getAttribute("banknbr");
		QueryWrapper<ProductType> queryWrapper=new QueryWrapper<ProductType>();
		productType.setBanknbr(banknbr);
		//筛选条件：网点编号
		 queryWrapper.eq(productType.getBanknbr()!=null&&productType.getBanknbr()!=0,"banknbr",productType.getBanknbr());
		List<ProductType> list = this.productTypeService.list(queryWrapper);
		return new DataGridView(list);
	}

	
	/**
	 * 查询设备类型下拉列表  根据网点编号
	 * @return
	 */
	@RequestMapping("loadAllTerminalForSelect")
	public DataGridView loadAllTerminalForSelect(){
		Terminal terminal = new Terminal();
		//存入网点编号和用户名放入session中
		 int banknbr = (int) WebUtils.getSession().getAttribute("banknbr");
		 terminal.setNetworkid(banknbr);
		//封装对象查询条件
		QueryWrapper<Terminal> queryWrapper=new QueryWrapper<>();
		//筛选条件：网点编号
		 queryWrapper.eq(terminal.getNetworkid()!=null&&terminal.getNetworkid()!=0,"network_id",terminal.getNetworkid());
		List<Terminal> list = this.terminalService.list(queryWrapper);
		return new DataGridView(list);
	}
	
	
	
	
	
	
	
	
}
